using NExt.Core.Model.Base;
namespace NExt.Core.Validator
{
    public abstract class ValidatorScript : IScript
    {
        private const string FUNCTION_TEXT_FORMAT = "function(value){{{0}}}";
        private const string BODY_TEXT_FORMAT = "return NExt.validator.{0}(value{1});";
        private readonly string textt;

        protected ValidatorScript(string externalFunctionName, params string[] parameterNames)
        {
            string parameterString = BuildParameterString(parameterNames);
            string bodyText = string.Format(BODY_TEXT_FORMAT, externalFunctionName, parameterString);
            textt = string.Format(FUNCTION_TEXT_FORMAT, bodyText);
        }

        public string text
        {
            get { return textt; }
        }

        private string BuildParameterString(string[] parameterNames)
        {
            if (parameterNames.Length == 0)
            {
                return string.Empty;
            }

            string parameterString = "," + string.Join(",", parameterNames);
            return parameterString;
        }
    }
}